package com.sc.test;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.navigation.NavigationView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Shen Chao on 2018-03-16-016 下午 12:26.
 */

public class DrawerLayoutActivity extends AppCompatActivity {
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.dl_drawerlayout)
    DrawerLayout dlDrawerlayout;
    @BindView(R.id.tl_toolbar)
    Toolbar tlToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawerlayout);
        ButterKnife.bind(this);
        //设置无titile,清单文件已设置
//        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
//        addHeaderView : 添加头部视图。
//        removeHeaderView : 移除头部视图。
//        getHeaderView : 获取指定位置的头部视图。
//        getHeaderCount : 获取头部视图的数量。
//        setItemBackground/setItemBackgroundResource : 设置菜单项的背景。
//        setItemIconTintList : 设置菜单项的图标色彩。
//        setItemTextColor : 设置菜单项的文字颜色。
//        setItemTextAppearance : 设置菜单项的文字样式。
        navView.setItemBackgroundResource(R.color.color2);
//        R.string.open|R.string.close  菜单显示和关闭时的描述信息
        setSupportActionBar(tlToolbar);
//        用ActionBarDrawerToggle 把DrawerLayout和Toolbar关联
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, dlDrawerlayout,
                tlToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        dlDrawerlayout.addDrawerListener(toggle);
        //设置左上角按钮为三横杠
        toggle.syncState();
        setNavigationView();

    }

    /**
     * 设置按返回键关闭侧滑菜单，而不是推出当前activity
     */
    @Override
    public void onBackPressed() {
        //GravityCompat.START,左边的侧滑菜单打开状态
        //GravityCompat.end，右边的侧滑菜单打开状态
        if (dlDrawerlayout.isDrawerOpen(GravityCompat.START)) {
            //如果打开，设置按返回关闭
            dlDrawerlayout.closeDrawer(GravityCompat.START);
        }
        super.onBackPressed();
    }

    private void setNavigationView() {
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_item1:
                        Toast.makeText(DrawerLayoutActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        dlDrawerlayout.closeDrawer(navView);
                        break;
                    case R.id.nav_item2:
                        Toast.makeText(DrawerLayoutActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        dlDrawerlayout.closeDrawer(navView);
                        break;
                    case R.id.nav_item3:
                        Toast.makeText(DrawerLayoutActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        dlDrawerlayout.closeDrawer(navView);
                        break;
                    case R.id.nav_item4:
                        Toast.makeText(DrawerLayoutActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        dlDrawerlayout.closeDrawer(navView);
                        break;
                    case R.id.nav_item5:
                        Toast.makeText(DrawerLayoutActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        dlDrawerlayout.closeDrawer(navView);
                        break;
                    case R.id.nav_item6:
                        Toast.makeText(DrawerLayoutActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        dlDrawerlayout.closeDrawer(navView);
                        break;
                }
                return true;
            }
        });
    }

}
